package com.bsj.travel.device.web;

import cn.hutool.extra.spring.SpringUtil;
import com.bsj.travel.cached.redis.RedisCached;
import com.bsj.travel.constants.RedisConstant;
import com.bsj.travel.def.common.DO.ProductTopicDO;
import com.bsj.travel.device.cache.TopicCache;
import com.bsj.travel.device.service.ProductService;
import com.bsj.travel.device.service.ProductTopicService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.context.annotation.Import;
import org.springframework.stereotype.Component;

import javax.annotation.PreDestroy;
import java.util.List;

/**
 * @author yinhao
 * @version 1.0
 * @description 工具初始化
 * @date 2023/11/16
 */
@Component
@Slf4j
@Import(SpringUtil.class)
public class Action implements CommandLineRunner {

    @Autowired
    private ProductService productService;

    @Autowired
    private RedisCached redisCached;

    @Autowired
    private ProductTopicService productTopicService;


    /**
     * 启动
     * @param args
     * @throws Exception
     */
    @Override
    public void run(String... args) throws Exception {
        TopicCache.doStart(productService, productTopicService, redisCached);

        // 初始化基础不可删减主题信息
        List<ProductTopicDO> baseTopic = productTopicService.queryTopicByProductId(0L);
        redisCached.set(RedisConstant.PRODUCT_BASE_TOPIC_KEY, baseTopic);
    }

    /**
     * 销毁时使用
     * @throws Exception
     */
    @PreDestroy
    public void destroy() throws Exception {}
}
